package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import model.*;

public class Gridv2 extends JPanel{
	
	int nbRows;
	int nbColums;
	Space2D universe;
	
	public Gridv2(Space2D universe){
		super();
		this.universe = universe;
		this.nbRows = universe.getDimX();
		this.nbColums = universe.getDimY();
		
	}
	
	public void paintComponent(Graphics g){
		
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		
		double nbColums_ = this.nbColums;
		double nbRows_ = this.nbRows;
		
		Double rowStep = new Double(this.getHeight() / nbRows_);
		Double columnStep = new Double(this.getWidth() / nbColums_);
	
		// Paint the grid
		g.setColor(Color.BLACK);
		this.paintGrid(g, rowStep, columnStep);
		
		// Paint the cells
		this.paintCells(g, rowStep, columnStep);
	}
	
	// Paint the grid
	public void paintGrid(Graphics g, Double rowStep, Double columnStep){
		
		// Draw the rows
		for (int i = 1; i<nbRows; i++){
			Double y_ = rowStep*i;
			int y = y_.intValue();
			g.drawLine(0, y, this.getWidth(), y);
			
		}
		
		// Draw the columns
		for (int i = 1; i<nbColums; i++){
			Double x_ = columnStep*i;
			int x = x_.intValue();
			g.drawLine(x, 0, x, this.getHeight());
		}
	}
	
	// Paint the cells
	public void paintCells(Graphics g, Double rowStep, Double columnStep){
		
		for (int i=0; i<this.universe.getDimX(); i++){
			for (int j=0; j<this.universe.getDimY(); j++){
				Point p = new Point(i, j);
				Cell cell = this.universe.getCell(p);
				if (cell != null){
					this.paintCell(g, rowStep, columnStep, p, cell);
				}
			}
		}
		
	}
	
	// Paint a cell
	public void paintCell(Graphics g, Double rowStep, Double columnStep, Point p, Cell cell){
		
		int intRowStep = rowStep.intValue();
		int intColumnStep = columnStep.intValue();
		
		Double x_ = columnStep*p.getY();
		int x = x_.intValue();
		
		Double y_ = rowStep*p.getX();
		int y = y_.intValue();
		
		//Paint the pheromone
		int pheromone = cell.getPheromone();
		//g.fillRect(x, y, intColumnStep+1, intRowStep+1);
		
		//Paint the Resource
		Resource resource = cell.getResource();
		if (resource != null){
			String type = resource.getType();
			if (type.equalsIgnoreCase("r1")){
				g.setColor(Color.red);
				g.fillOval(x+intColumnStep/2, y+intRowStep/2, intColumnStep/2, intRowStep/2);
			} else if (type.equalsIgnoreCase("r2")){
				g.setColor(Color.blue);
				g.fillOval(x+intColumnStep/2, y+intRowStep/2, intColumnStep/2, intRowStep/2);
			} else if (type.equalsIgnoreCase("r3")){
				g.setColor(Color.green);
				g.fillOval(x+intColumnStep/2, y+intRowStep/2, intColumnStep/2, intRowStep/2);
			}
		}
		
		//Paint the ant
		Ant ant = cell.getAnt();
		if (ant != null){
			try {
				Image img = ImageIO.read(new File("ant.gif"));
				g.drawImage(img, x, y, intColumnStep, intRowStep, this);
				//Pour une image de fond
				//g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}	
	}
	
	public static void main(String[] args) throws Exception{
		JFrame frame = new JFrame("Test");
		
		Space2D universe = new Space2D(30, 30);
		Point p1 = new Point(0,0);
		Point p2 = new Point(7,3);
		Point p3 = new Point(4,5);
		Point p4 = new Point(7,9);
		
		Point p5 = new Point(2,2);
		
		Ant ant1 = new Ant(universe, p5);
		Ant ant2 = new Ant(universe, p2);
		Resource r1 = new Resource(universe, p1, "r1");
		Resource r2 = new Resource(universe, p3, "r2");
		Resource r3 = new Resource(universe, p4, "r3");
		
		universe.addAnt(ant1);
		universe.addAnt(ant2);
		universe.addResource(r1);
		universe.addResource(r2);
		universe.addResource(r3);
		
		Gridv2 grid = new Gridv2(universe);
		
		frame.setSize(500, 500);
		frame.setLocationRelativeTo(null);               
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.add(grid);
		
		frame.setVisible(true);
		
		while(true){
			ant1.step();
			grid.repaint();
			Thread.sleep(10);
		}
		
	}

}
